set more off

*** read data ***

use "reassessments", clear

merge 1:1 swis_code year using "switchers"
drop _m

merge 1:1 swis_code year using "rates"
drop _m

duplicates drop muni_name county_name year, force
merge 1:1 muni_name county_name year using "census demographics"
drop _m

merge 1:1 muni_name county_name year using "finances clean"
drop _m

merge 1:1 muni_name county_name year using "employment"
drop _m

drop if mi(swis_code)
keep if year > 1986 & year < 2012

*** recode for interpretation ***

gen elected = 1-D
label variable elected "Elected assessor"

keep if !mi(swis_code)
xtset swis_code year

#delimit;

local X1 
	lpopulation 
	lmedianincome 
	pct65plus 
	pctwhite 
	pctunemployed 
	pctrenter 
	pctagricuture
	;

#delimit cr

local X2 ltotalrevenue pctptax surpluspcp totalftequivalent

local X `X1' `X2'

foreach x in `X' {
	su `x'
	replace `x' = (`x' - r(min)) / (r(max) - r(min))
}

gen reassXelected = reass*elected

*** do the regressions ***

eststo clear

eststo: xi: areg equal reasse elected reassXelected i.year, a(swis_code) cl(swis_code)
preserve
	replace reassessed = 1
	replace elected = 1
	replace reassXelected = 1
	predict yhat, xb
	su yhat
restore
estadd scalar y1 = r(mean)
preserve
	replace reassessed = 1
	replace elected = 0
	replace reassXelected = 0
	predict yhat, xb
	su yhat
restore
estadd scalar y0 = r(mean)
lincom (_cons + reasse + elected + reassXelected) - (_cons + reasse)
estadd scalar myb = r(estimate)
estadd scalar myse = r(se)

eststo: xi: areg equal reasse elected reassXelected i.year `X1', a(swis_code) cl(swis_code)
preserve
	replace reassessed = 1
	replace elected = 1
	replace reassXelected = 1
	predict yhat, xb
	su yhat
restore
estadd scalar y1 = r(mean)
preserve
	replace reassessed = 1
	replace elected = 0
	replace reassXelected = 0
	predict yhat, xb
	su yhat
restore
estadd scalar y0 = r(mean)
lincom (_cons + reasse + elected + reassXelected) - (_cons + reasse)
estadd scalar myb = r(estimate)
estadd scalar myse = r(se)

eststo: xi: areg equal reasse elected reassXelected i.year `X1' `X2', a(swis_code) cl(swis_code)
preserve
	replace reassessed = 1
	replace elected = 1
	replace reassXelected = 1
	predict yhat, xb
	su yhat
restore
estadd scalar y1 = r(mean)
preserve
	replace reassessed = 1
	replace elected = 0
	replace reassXelected = 0
	predict yhat, xb
	su yhat
restore
estadd scalar y0 = r(mean)
lincom (_cons + reasse + elected + reassXelected) - (_cons + reasse)
estadd scalar myb = r(estimate)
estadd scalar myse = r(se)

label variable reassessed "Reassessed"
label variable reassXelected "Reassessed $\times$ elected"

#delimit;

esttab
	using "table A6.tex"
	,
		replace
		cells(b(fmt(2) star) se(par))
		eqlabels(none)
		nomtitles
		collabels(none)
		gap
		keep(reassessed elected reassXelected)
		label
		mlabels(none)
		nonotes
		booktabs
		width(\hsize)
		indicate("Demographic controls = lpopulation" "Fiscal controls = ltotalrevenue")
		stats(

			N
			y1
			y0
			myb
			myse
			,
				label(

					"Observations"
					"Predicted accuracy after reassessment, elected"
					"Predicted accuracy after reassessment, appointed"
					"Difference"
					"Standard error"
						
				)
				fmt(%20.0fc %20.02fc %20.02fc %20.02fc %20.02fc)
		)
		
		;
	
#delimit cr

shell "tabler.sh" "table A6.tex" "Assessment accuracy" "Town"

